package com.zoo.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.zoo.common.Pagination;
import com.zoo.entity.ProductType;
import com.zoo.service.ProductTypeBiz;

@Controller
@Scope("prototype")
public class ProductTypeAction {
	@Resource
	private ProductTypeBiz service ;
	private String message ;
	private List<ProductType> types ;
	private ProductType type ;
	private Pagination pagination; 
    private int pageNo = 1;
	private String parentID  ;

	public List<ProductType> getTypes() {
		return types;
	}
	public void setTypes(List<ProductType> types) {
		this.types = types;
	}
	public ProductType getType() {
		return type;
	}
	public void setType(ProductType type) {
		this.type = type;
	}
	public void setService(ProductTypeBiz service) {
		this.service = service;
	}
	public Pagination getPagination() {
		return pagination;
	}
	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}
	public int getPageNo() {
		return pageNo;
	}
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}	
	public String getParentID() {
		return parentID;
	}
	public void setParentID(String parentID) {
		this.parentID = parentID;
	}
	/**  
     * 产品分类列表显示  
     */   
    public String list(){
    	String jpql = "parent is null" ;
    	Object[] params = null ;
    	ProductType parent = null ;
    	if(type!=null && type.getId()>0){
    		jpql = "parent.id=?" ;
    		params = new Object[]{type.getId()};
    		parent = service.findByID(type.getId());
    	}
    	types = service.findAll(jpql, params);
    	String urlTemp = type==null ? "producttype_list.do" : "producttype_list.do?type.id="+type.getId();
    	pagination = new Pagination(pageNo, 12, types.size(),5,urlTemp,"pagenum");  
    	types = pagination.getPageDate(types);
    	List<ProductType> menutypes = new ArrayList<ProductType>();
    	while(parent!=null){
    		menutypes.add(parent);
    		parent = parent.getParent();
    	}
    	ActionContext.getContext().getSession().put("menutypes", menutypes); 	
    	return "list";   
    }   
    /**  
     * 产品分类添加界面  
     */   
    public String addUI(){
        return "add";   
    }
    /**
     * 产品分类查看界面
     */
    public String view(){
    	type = service.findByID(type.getId());
    	return "view" ;
    }
    /**  
     * 人员添加  
     */   
    public String add(){   
    	if(null==parentID || "".equals(parentID)){
    		type.setParent(null);
    	}else{
    		type.setParent(service.findByID(Integer.parseInt(parentID)));
    	}
    	service.save(this.type);   
        this.message="产品分类添加成功";  
        ActionContext.getContext().getSession().put("url","producttype_list.do");
        return "success";   
    } 
    /**
     * 产品分类编辑界面
     */
    public String editUI(){
    	type = service.findByID(type.getId());    	
    	return "editUI" ;
    }
    /**
     * 产品分类编辑
     */
    public String edit(){
    	service.update(this.type);   
        this.message="产品分类修改成功";   
        return "success";
    }
}
